package com.iplant.mes.controller.sfc;

import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.mes.service.SFCFrockBindService;
import com.iplant.mes.service.SFCService;
import com.iplant.mes.service.po.sfc.SFCAssemblyRecord;
import com.iplant.mes.service.po.sfc.SFCTaskSelf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 工件组装记录
 *
 * @author YouWang·Peng
 * 2023-12-1 13:47:01
 */
@RestController
@RequestMapping("/api/SFCAssemblyRecord")
public class SFCAssemblyRecordController extends BasicController<SFCAssemblyRecord> {
    private static final Logger logger = LoggerFactory.getLogger(SFCAssemblyRecordController.class);

    public SFCAssemblyRecordController() {
        super(SFCAssemblyRecord.class);
    }

    @Autowired
    SFCService mSFCService;

    @Autowired
    SFCFrockBindService mSFCFrockBindService;

    /**
     * 工件组装
     */
    @PostMapping("/WorkpieceAssembly")
    public Object WorkpieceAssembly(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            String wSourceCodes = StringUtils.parseString(wParam.get("SourceCodes"));
            String wTargetCode = StringUtils.parseString(wParam.get("TargetCode"));
            //组装类型（若为1，则表示芯子与出厂条码的绑定，否则即为工件的正常绑定）
            int wAssemblyType = StringUtils.parseInt(wParam.get("AssemblyType"));
            SFCTaskSelf wSFCTaskSelf = CloneTool.Clone(wParam.get("SFCTaskSelf"), SFCTaskSelf.class);

            if (StringUtils.isEmpty(wSourceCodes) || StringUtils.isEmpty(wTargetCode) || wSFCTaskSelf == null || wSFCTaskSelf.ID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mSFCService.SFC_WorkpieceAssembly(wLoginUser, wSourceCodes,
                    wTargetCode, wSFCTaskSelf, wAssemblyType);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 替换工件码
     */
    @GetMapping("/ReplacementJobCode")
    public Object ReplacementJobCode(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wSFCAssemblyRecordID = StringUtils.parseInt(request.getParameter("AssemblyRecordID"));
            String wNewJobCode = StringUtils.parseString(request.getParameter("NewJobCode"));
            if (wSFCAssemblyRecordID <= 0 || StringUtils.isEmpty(wNewJobCode)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mSFCFrockBindService.SFC_ReplacementJobCode(wLoginUser,
                    wSFCAssemblyRecordID, wNewJobCode);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
